package com.kpx.app.view;

import java.io.IOException;

import android.content.Context;
import android.hardware.Camera;
import android.view.SurfaceHolder;
import android.view.SurfaceHolder.Callback;
import android.view.SurfaceView;

/**
 * 录像预览
 * 
 * @Description TODO
 * @author CodeApe
 * @version 1.0
 * @date 2013-11-7
 * @Copyright: Copyright (c) 2013 Shenzhen Tentinet Technology Co., Ltd. Inc.
 *             All rights reserved.
 * 
 */
public class CameraPreview extends SurfaceView implements Callback {

	/** 刷新界面的回调 */
	private SurfaceHolder mHolder;
	/** 摄像头对象 */
	public Camera mCamera;

	public CameraPreview(Context context, Camera camera) {
		super(context);
		mCamera = camera;

		// 安装一个SurfaceHolder.Callback
		mHolder = getHolder();
		mHolder.addCallback(this);
		// 针对低于3.0的Android
		mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
	}

	public void surfaceCreated(SurfaceHolder holder) {
		try {
			if(mCamera != null )
			{
			mCamera.setPreviewDisplay(holder);
			mCamera.startPreview();
			}
		} catch (IOException e) {
		}
	}

	public void surfaceDestroyed(SurfaceHolder holder) {
		// TODO
	}

	public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {

		if (mHolder.getSurface() == null) {
			// 预览surface不存在
			return;
		}

		// 更改时停止预览
		try {
			mCamera.stopPreview();
		} catch (Exception e) {

		}

		// 在此进行缩放、旋转和重新组织格式
		// 以新的设置启动预览
		try {
			mCamera.setPreviewDisplay(mHolder);
			mCamera.startPreview();
		} catch (Exception e) {
			e.getMessage();
		}
	}

}
